package ge.basicActors;

import ge.engine.Log;
import ge.engine.PositionableActor;
import ge.engine.PositionableArea;
import ge.modules.events.ButtonEvent;
import ge.modules.events.IOListener;
import ge.modules.logics.SquareCollide;

public class Button extends PositionableActor implements PositionableArea
{
	private IOListener raton;
	private SquareCollide areaButton;
	 
	private DrawableButton graphic;
 
	private boolean pressed;
	private String buttonKey;
	private int id;
	/**
	 * 	Constructs a new button. A button is always a logic square, and it is independent from
	 * 	its representation. By default, action click is with LEFT button.
	 * @param idButton 0 is no id. This id is used by the ButtonListener to identify each button.
	 * @param width width of the button
	 * @param height height of the button
	 * @param graphic Graphical representation of the button.
	 */
	public Button(int idButton,double width, double height,DrawableButton graphic)
	{
		super();
		raton = new IOListener();
		areaButton = new SquareCollide(width, height);
		new ButtonEvent(idButton, this);
		buttonKey =  "LEFT";
		this.graphic = graphic;
		areaButton.setFixed(true);
		pressed = false;
		id = idButton;
		add(raton);
		attach(areaButton);
		attach(graphic);
		
	}
	
	/**
	 * 
	 * @return True if the button is now pressed
	 */
	public boolean buttonPressed()
	{
		return pressed;
	}
	/**
	 * 
	 * @return Button action key
	 */
	public String getButtonKey() {
		return buttonKey;
	}

	/**
	 * 
	 * @param buttonKey A new button action key, it have to follow the rules from IOListener button keys
	 */
	public void setButtonKey(String buttonKey) {
		this.buttonKey = buttonKey;
	}

	/**
	 * 
	 * @return Graphical representation of the button
	 */
	public DrawableButton getGraphic() {
		return graphic;
	}

	/**
	 * 	
	 * @param graphic New graphical representation
	 */
	public void setGraphic(DrawableButton graphic) 
	{
		remove(graphic);
		this.graphic = graphic;
		attach(graphic);
	}
	/**
	 * 	
	 * @return If the logic area is fixed
	 */
	public boolean isFixed() {
		return areaButton.isFixed();
	}
	/**
	 * 	
	 * @param fixed Sets the logic area of the button fixed to the camera.
	 * 	It doesn't fix the graphic button.
	 */
	public void setFixed(boolean fixed) {
		areaButton.setFixed(fixed);
	}

	@Override
	public void update()
	{ 
		super.update();
		pressed = false;
		
		if(raton.mouseClickOn(areaButton, buttonKey))
			pressed = true;
		else if(raton.mouseOverOn(areaButton) && raton.mouseDown(buttonKey))
			graphic.mouseClick();
		else if(raton.mouseOverOn(areaButton))
			graphic.mouseOver();
		else
			graphic.buttonidle();
		
		//Log.write(id+" "+pressed);
	
	}
	@Override
	public void draw()
	{
		 super.draw();	
	}
 
	@Override
	public double getWidth() {
		return  areaButton.getWidth();
	}


	@Override
	public double getHeight() {
		 return areaButton.getHeight();
	}


	@Override
	public void setArea(double width, double height) {
		areaButton.setArea(width, height);
		
	}
	
	
	
	

}
